<template>
    <div>
        <el-row>
            <el-col span="4">
             
            </el-col>
            <el-col span="20">
                <div class="crumbs">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 权限列表 </el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <div class="container">
                    <div class="handle-box">
                        <el-button type="primary" icon="el-icon-star-off" class="handle-del mr10" @click="addPermiss"
                            >添加权限</el-button
                        >
                        <el-input v-model="query.UserName" placeholder="用户名" class="handle-input mr10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
                    </div>
                    <el-table
                        :data="tableData"
                        style="width: 100%; margin-bottom: 20px"
                        row-key="Id"
                        border
                        default-expand-all
                        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
                    >
                        <el-table-column prop="PerName" label="权限名称" sortable width="180"> </el-table-column>
                        <el-table-column prop="CreateTime" label="创建时间" sortable width="180"> </el-table-column>
                        <el-table-column prop="IconName" label="图标" sortable width="180"> </el-table-column>
                        <el-table-column prop="PerUrl" label="路径" sortable width="180"> </el-table-column>
                         <el-table-column  label="操作" sortable width="180">
                            <template slot-scope="scope">
                               <el-link  type="success" @click="updpermiss(scope.row)">编辑</el-link>
                               <el-link  type="danger" @click="delpermiss(scope.row)">删除</el-link>
                            </template>
                             </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                            background
                            layout="total, prev, pager, next"
                            :current-page="query.pageIndex"
                            :page-size="query.pageSize"
                            :total="pageTotal"
                            @current-change="handlePageChange"
                        ></el-pagination>
                    </div>
                </div>
            </el-col>
        </el-row>
        <!--添加权限-->
        <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  >
  <span>添加权限</span>
  <el-form ref="form" :model="form" label-width="80px">
     <el-form-item label="权限名称">
    <el-input v-model="form.PerName"></el-input>
    </el-form-item>
    <el-form-item label="上级">
    <template>
        <treeselect  v-model="form.Pid" :multiple="true" :options="options" />
    </template>
  </el-form-item>
    <el-form-item label="权限图标">
    <el-input v-model="form.IconName"></el-input>
    </el-form-item>
    <el-form-item label="权限Url">
    <el-input v-model="form.PerUrl"></el-input>
  </el-form-item>
   <el-form-item label="排序Id">
    <el-input v-model="form.SortNo"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="addpermission">立即创建</el-button>
    <el-button>取消</el-button>
  </el-form-item>
    </el-form>
</el-dialog>
<!--添加权限-->
        <el-dialog
  title="提示"
  :visible.sync="upddialogVisible"
  width="30%"
  >
  <span>编译权限</span>
  <el-form ref="forms" :model="forms" label-width="80px">
     <el-form-item label="权限名称">
    <el-input v-model="forms.PerName"></el-input>
    </el-form-item>
    <el-form-item label="上级">
    <template>
        <treeselect  v-model="forms.Pid" :normalizer="normalizer" :multiple="false" :options="options" />
    </template>
  </el-form-item>
    <el-form-item label="权限图标">
    <el-input v-model="forms.IconName"></el-input>
    </el-form-item>
    <el-form-item label="权限Url">
    <el-input v-model="forms.PerUrl"></el-input>
  </el-form-item>
   <el-form-item label="排序Id">
    <el-input v-model="forms.SortNo"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="updpermission">编辑</el-button>
    <el-button>取消</el-button>
  </el-form-item>
    </el-form>
</el-dialog>
    </div>
</template>

<script>
import { userrole, rolelist, addrelation, deleterelation, permission, addpermiss, rolepermiss, findpermiss,navpermiss,addpermission,delpermission,backpermission,updpermission } from '@/api/role'
import Treeselect from '@riophae/vue-treeselect' //树下拉
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
     components: { Treeselect },
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                UserName: '',
                pageIndex: 1,
                pageSize: 10
            },
            roleData: [],
            tableData: [
             
            ],
            upddialogVisible:false,
            multipleSelection: [],
            delList: [],
            dialogVisible:false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            data: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            value:null,
            options:[],
            forms:{}
        };
    },
    created() {
        this.getData();
        this.PermissionList();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            navpermiss().then((res) => {
                //这里的res其实就是res.ata对象，因为拦截器里面返回了res.data
                console.log(res);
                this.tableData = res.data;
                this.tableData.children=res.data.children
                // this.tableData = res.data.List;
                // this.pageTotal = res.data.Total;
                //this.query.name=res.data.UserName
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        normalizer(node) {
      return {
        id: node.Pid,
        label: node.PerName,
        children: node.children,
      }
    },
        //编辑权限
        updpermiss(row){
          console.log("行信息",row);
          this.upddialogVisible=true;

          this.options=[{Pid:0,PerName:'根节点',children:this.tableData}]

          backpermission({id:row.Id}).then(res=>{
            this.forms=res.data;
            //this.forms.Pid[0]=res.data.Pid
            console.log("反填的信息",this.forms);
          })
        },
        //确定编辑   
        updpermission(){
            updpermission(this.forms).then(res=>{
                if(res.code==200)
                {
                   this.$message.success('编辑成功');
                   this.upddialogVisible=false;
                   this.getData();
                }
            })
        },
       
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        AddUser() {
            addUser(this.form).then((res) => {
                if (res != null) {
                    this.$message.success('添加成功');
                    this.AddeditVisible = false;
                    this.getData();
                }
            });
        },
        //树形
        handleNodeClick(data) {
            console.log('树数据', data);
        },    
        // 编辑操作
        handleEdit(index, row) {
            this.editVisible = true;

            this.idx = index;
            this.form.Id = row.Id;
            this.form.CreateTime = row.CreateTime;
            // this.editVisible = true;
        },
        // 保存编辑
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
      //树下拉
        PermissionList() {
            permission().then((res) => {
                if (res.code == 200) {
                    this.options = res.data;
                }
            });
        },
        //添加权限
        addPermiss(){
            this.dialogVisible=true;
        },
        addpermission(){
            this.form.Pid=this.form.Pid[0]
            console.log("父id",this.form.Pid);
            console.log("添加权限的信息",this.form);
            addpermission(this.form).then(res=>{
                if(res.code==200)
                {
                  this.$message.success('添加权限成功');
                  this.dialogVisible=false;
                  this.getData();
                }
            })
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
